                                                                                                                                                                                    <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="../../assets/libs/layui/css/layui.css">
</head>
<body>
<form class="layui-form" action="">
    <div class="layui-form-item">
        <label class="layui-form-label">标题</label>
        <div class="layui-input-block">
            <input type="text" name="title" required  lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">作者</label>
        <div class="layui-input-block">
            <input type="text" name="author" required  lay-verify="required" placeholder="请输入作者" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">摘要</label>
        <div class="layui-input-block">
            <textarea name="summary" placeholder="请输入内容" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-input-block">
        <button type="button" class="layui-btn" id="test1">
            <i class="layui-icon">&#xe67c;</i>上传图片
        </button>
        <div class="layui-upload-list">
            <img class="layui-upload-img" id="demo1">
            <p id="demoText"></p>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">照片路径</label>
        <div class="layui-input-block">
            <input type="text" id="path" name="path" lay-verify="title" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">照片名称</label>
        <div class="layui-input-block">
            <input type="text" id="pname" name="pname" lay-verify="title" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">创作时间</label>
            <div class="layui-input-block">
                <input type="text" name="time" id="date1" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">内容</label>
            <div  class="layui-input-block">
                <textarea name="content" id="content" autocomplete="off" class="layui-textarea"></textarea>
            </div>
    </div>
    <div class="layui-form-item">
    <div class="layui-input-block">
        <button class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
    </div>
</form>
</body>
<script src="../../assets/libs/layui/layui.js"></script>
<script src="../../assets/js/my.js"></script>
<script>
    layui.use(['form','layer','upload','laydate','jquery'], function () {
        var form = layui.form
            , layer = layui.layer
            , $ = layui.jquery
            , upload = layui.upload
            , laydate=layui.laydate;

        laydate.render({
            elem:'#date1'
        })
        upload.render({
            elem: '#test1' //绑定元素
            , url:host+'/newsservlet/upload' //上传接口
            , done: function (res) {
                if (res.code == 0) {
                    layer.msg('上传成功', {icon: 1});
                    $('#path').val(res.path);
                    $('#pname').val(res.path.substr(47))
                }
            }
            , error: function () {
                //请求异常回调
            }
        })


        form.on('submit(demo1)', function (data) {
            layer.alert(JSON.stringify(data.field), {
                title: '最终的提交信息'
            })
            $.get(host + '/newsservlet/save', data.field, function (r) {
                if (r.code == 0) {
                    layer.msg("添加成功", {icon: 1})
                } else {
                    layer.msg("添加失败", {icon: 2})
                }
            })
        });
    })
</script>
</html>